package com.example.demo.observer;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class OrdinalDemoListener implements SmartApplicationListener {

    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
        //只有UserRegisterEvent监听类型才会执行下面逻辑
        return eventType == DemoEvent.class;
    }

    @Override
    public boolean supportsSourceType(Class<?> sourceType) {
        return true;
        //只有在UserService内发布的UserRegisterEvent事件时才会执行下面逻辑
//        return aClass == UserService.class;
    }

    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        //转换事件类型
        DemoEvent userRegisterEvent = (DemoEvent) event;
        //获取注册用户对象信息
        String msg = userRegisterEvent.getMessage();
        //.../完成注册业务逻辑
        System.out.println("有序执行0000000=======" + msg);
    }
}
